package TangramCore;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.InputStream;
import org.freehep.graphicsio.emf.*;
public class HladinaVektor extends Hladina {
    EMFRenderer img;
    BufferedImage buf;
    boolean hidden;
    static int count = 0;
    int align;
    boolean maxSize;
    boolean cleared = true;
    Rectangle2D rect = new Rectangle2D.Double();
    Rectangle act;
    
    public HladinaVektor(String filename, Class<? extends Object> res, int a, boolean ms, Rectangle rp) {
        relpos = rp;
        align = a;
        maxSize = ms;
        try{
            InputStream in = res == null ? new FileInputStream(filename) : res.getResourceAsStream(filename);
            img = new EMFRenderer(new EMFInputStream(in));
        } catch(Exception ex){ex.printStackTrace();}
        hidden = false;
    }
    
    @Override
    void clear(Graphics2D g) {
    }
    
    @Override    
    void draw(Graphics2D g) {
        if(hidden)
            return;
        Shape oldClip = g.getClip();
        AffineTransform old = g.getTransform();
        g.clip(pos);//TODO ?? zbytecne ale precniva
        g.setTransform(at);
        img.paint(g);
        g.setTransform(old);
        g.setClip(oldClip);
    }
    
    @Override    
    public void hide() {
        hidden = true;
        rect.setRect(pos);
    }
    
    @Override
    public void resize(Rectangle r) {
        super.resize(r);
        //pos = r;
        cleared = true;
        resize();
    }
   
    @Override
    public void resize(){

        //double pok = img.getMeterLimit();
        //AffineTransform atpok = img.getPathTransform().getScaleInstance(pok, pok);
        
        Dimension dim = img.getSize();
        
        //double wo = img.getSize().getgetWidth(), w = wo;
        //double ho = img.getSize().getHeight(), h = ho;
        double w = dim.width, wo = 800;
        double h = dim.height, ho = 600;
        if(maxSize){
            double kx = (double)pos.width/wo, ky = (double)pos.height/ho;
            if(kx < ky){
                w = pos.width;
                h = ho / wo * w;
            } else{
                h = pos.height;
                w = wo / ho * h;
            }
            //h = pos.height;
        }
        double x = pos.x + (pos.width - w) / 2;
        double y = pos.y + (pos.height - h)/2;
        if( (align & TOP) != 0 )
            y = pos.y;
        else if( (align & BOTTOM) != 0)
            y = pos.y + pos.height - h;
        if( (align & LEFT) != 0 )
            x = pos.x;
        else if( (align & RIGHT) != 0)
            x = pos.x + pos.width - w;
        at = AffineTransform.getTranslateInstance(x,y);
        at.scale(w/dim.width, h/dim.height);

        //img.setMapModeIsotropic(true);
        //img.fixViewportSize();
        //img.setMapModeTransform(AffineTransform.getScaleInstance(1, 1));
        //img.setWindowOrigin(new Point(0, 0));
        //img.setViewportOrigin(new Point((int)x, (int)y));
        //img.setWindowSize(new Dimension(pos.width, pos.height));
        //img.setViewportSize(new Dimension((int)w,(int)h));
        //buf = null;
    }
    
    @Override    
    public void show() {
        hidden = false;
        cleared = true;
    }
    
    @Override    
    void validate() {
        if(cleared)
            addRect2D(rect, pos);
        addRect2D(obr.rectUpdate, rect);
        rect.setRect(0,0,0,0);
        cleared = false;
    }
    
    public void toggleVisibility(){
        if(hidden)
            show();
        else
            hide();
    }

}
